package com.lxl.xm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxl.xm.common.ErrorCode;
import com.lxl.xm.exception.BusinessException;
import com.lxl.xm.model.entity.StudentTask;
import com.lxl.xm.model.entity.Task;
import com.lxl.xm.service.StudentTaskService;
import com.lxl.xm.mapper.StudentTaskMapper;
import com.lxl.xm.service.TaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author 86135
* @description 针对表【student_task(学生任务参与表)】的数据库操作Service实现
* @createDate 2024-03-09 14:14:17
*/
@Service
public class StudentTaskServiceImpl extends ServiceImpl<StudentTaskMapper, StudentTask>
    implements StudentTaskService{

    @Resource
    StudentTaskMapper studentTaskMapper;


    @Override
    public Long countCompletedStudentsByTaskId(Long taskId) {
        if (taskId == null || taskId < 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"任务ID错误");
        }
        //查询有没有该任务
//        Task task = taskService.getById(taskId);
//        if (task == null){
//            throw new BusinessException(ErrorCode.PARAMS_ERROR,"没有该任务");
//        }
        return studentTaskMapper.countCompletedStudentsByTaskId(taskId);
    }

    /**
     * 学生完成主线任务
     * 学生用户信息表需要添加完成信息记录
     * 完成任务的情况是1|1,1|2,1|3,则完成的情况是完成了主线1的1，2，3号任务，方便进行学生完成任务的统计
     * @param studentId
     * @param taskId
     * @return
     */
    @Override
    public Boolean studentCompleteTask(Long studentId, Long taskId) {
        //记录是否存在
        LambdaQueryWrapper<StudentTask> studentTaskLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentTaskLambdaQueryWrapper.eq(StudentTask::getStudentId,studentId)
                .eq(StudentTask::getTaskId,taskId);
        StudentTask studentTask = this.getOne(studentTaskLambdaQueryWrapper);
        if (studentTask == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"学生任务记录不存在");
        }
        LambdaUpdateWrapper<StudentTask> studentTaskLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        studentTaskLambdaUpdateWrapper.eq(StudentTask::getStudentId,studentId)
                .eq(StudentTask::getTaskId,taskId)
                .setSql("is_completed = 1");
        //todo 完成任务的情况是1|1,1|2,1|3,则完成的情况是完成了主线1的1，2，3号任务，方便进行学生完成任务的统计
        return this.update(studentTaskLambdaUpdateWrapper);
    }

    /**
     * 查询学生是否参加主任务并且是否完成了主任务
     * @param studentId
     * @param taskId
     * @return
     */
    @Override
    public Boolean isStudentJoinTaskAndCompleted(Long studentId, Long taskId) {
        //是否参加并且完成
        LambdaQueryWrapper<StudentTask> studentTaskLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentTaskLambdaQueryWrapper.eq(StudentTask::getStudentId,studentId)
                .eq(StudentTask::getTaskId,taskId)
                .eq(StudentTask::getIsCompleted,1);
        return this.getOne(studentTaskLambdaQueryWrapper) != null;
    }
}




